package com.nbsaas.boot.app.rest.convert;

import com.nbsaas.boot.app.api.domain.simple.AppMenuSimple;
import com.nbsaas.boot.app.data.entity.AppMenu;
import com.nbsaas.boot.rest.api.Converter;
import lombok.Data;

import java.util.stream.Collectors;

/**
 * 菜单-列表对象转换器
 */

@Data
public class AppMenuSimpleConvert implements Converter<AppMenuSimple, AppMenu> {


    private int fetch;


    @Override
    public AppMenuSimple convert(AppMenu source) {
        AppMenuSimple result = new AppMenuSimple();


        try {
            if (source.getParent() != null) {
                result.setParent(source.getParent().getId());
            }
        } catch (Exception ignored) {
        }

        result.setCode(source.getCode());
        result.setCatalog(source.getCatalog());
        result.setNum(source.getNum());
        result.setIcon(source.getIcon());
        result.setPath(source.getPath());
        result.setRouter(source.getRouter());
        result.setSortNum(source.getSortNum());
        result.setId(source.getId());
        result.setRgt(source.getRgt());
        result.setLastDate(source.getLastDate());

        try {
            if (source.getApp() != null) {
                result.setApp(source.getApp().getId());
            }
        } catch (Exception ignored) {
        }

        result.setCreator(source.getCreator());
        result.setDisableIcon(source.getDisableIcon());
        result.setSearchType(source.getSearchType());

        try {
            if (source.getApp() != null) {
                result.setAppName(source.getApp().getName());
            }
        } catch (Exception ignored) {
        }

        result.setPermission(source.getPermission());
        result.setAddDate(source.getAddDate());

        try {
            if (source.getParent() != null) {
                result.setParentName(source.getParent().getName());
            }
        } catch (Exception ignored) {
        }

        result.setDepth(source.getDepth());
        result.setTenantId(source.getTenantId());
        result.setName(source.getName());
        result.setIds(source.getIds());
        result.setMenuType(source.getMenuType());
        result.setLft(source.getLft());

        result.setLabel(source.getName());
        result.setValue("" + source.getId());
        if (source.getChildren() != null && source.getChildren().size() > 0) {
            if (fetch != 0) {
                result.setChildren(source.getChildren().stream().map(this).collect(Collectors.toList()));
            }
        } else {
        }

        if (result.getCode() == null) {
            result.setCode("");
        }

        return result;
    }

}